package com.cdty.chess.service.impl;

import com.cdty.chess.model.Withdraw;
import com.cdty.chess.service.WithdrawService;
import com.cdty.chess.vo.WithdrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WithdrawServiceImpl implements WithdrawService {

    @Autowired
    NamedParameterJdbcTemplate template;

    @Override
    public String apply(Withdraw qc) {
        String sql = """
                    insert into withdraw (
                        id,uid,uname,score,create_time,status,unumber
                    )
                    values(uuid(),:uid,:uname,:score,now(),'1',:unumber)
                """;
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("uid", qc.getUid());
        params.addValue("uname", qc.getUname());
        params.addValue("score", qc.getScore());
        params.addValue("unumber", qc.getUnumber());
        template.update(sql,params);
        return "申请成功";
    }

    @Override
    public Map<String,Object> getList(String uid, String uname, String searchDate, Integer page, Integer size) {
        String sql = """
                    select wd.*,cu.balance from withdraw wd
                             inner join cuser cu on wd.uid=cu.uid
                             where 1=1
                """;
        MapSqlParameterSource params = new MapSqlParameterSource();

        if(StringUtils.hasText(uid)){
            sql += " and wd.uid = :uid";
            params.addValue("uid", uid);
        }
        if(StringUtils.hasText(uname)){
            sql += " and uname like concat('%',:uname,'%')";
            params.addValue("uname", uname);
        }
        /*if(StringUtils.hasText(status)){
            sql += " and status = :status";
            params.addValue("status", status);
        }*/
        if(searchDate!=null){
            sql += " and Date(wd.create_time) = :searchDate";
            params.addValue("searchDate", searchDate);
        }
        String countsql = "SELECT count(1) total FROM ("+sql+") a ";
        sql += " order by create_time desc LIMIT :size OFFSET :offset";
        int offset = (page - 1) * size;
        params.addValue("size", size);
        params.addValue("offset", offset);
        Map<String,Object> result = new HashMap<>();
        List<Map<String, Object>> mapList = template.queryForList(sql, params);
        Map<String, Object> countMap = template.queryForMap(countsql, params);
        result.put("total", countMap.get("total"));
        result.put("dataList", mapList);
        return result;
    }

    @Override
    public String update(String id) {
        String sql =" update withdraw set status = '2',up_time=now() where id=:id";
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("id", id);
        template.update(sql,params);
        return "";
    }
}
